#include "TrackBirdMgr.h"
#include "TrackBird.h"
#include "KinectWinMgr.h"
#include "resourcemanager.h"
TrackBirdMgr::TrackBirdMgr()
{
	mCurrentCalledIndex = 0;
	mCenterPoint = QVector3D(160,450,0);
	mRaidus = 60.0f;
	for (int i=0;i<MAX_TRACKED_COUNTS;i++)
	{
		QVector3D  increamtal=QVector3D(mRaidus*cos(72.0f*i*M_PI/180),mRaidus*sin(72.0f*i*M_PI/180),0);
		//QVector3D  birdInitPos = mCenterPoint+increamtal;
		QVector3D birdInitPos = QVector3D(mCenterPoint.x() - i * 30, mCenterPoint.y(), 0);
		mBirdObjList.push_back(new TrackBird(new Bird(ResourceManager::getSingleton().GetResPath(BirdNormalPng).c_str(),
			birdInitPos, 50, 0, 0),"BirdFly", BIRD_ID));
		mBirdInitPosList.push_back(birdInitPos);
	}
}
TrackBirdMgr::~TrackBirdMgr()
{
	for (std::vector<TrackBird *>::iterator it = mBirdObjList.begin(); it != mBirdObjList.end(); it ++) 
	{
		if (NULL != *it) 
		{
			delete *it; 
			*it = NULL;
		}
	}
	mBirdObjList.clear();
}

int  TrackBirdMgr::GetCountsByState(eBirdState e)
{
	int ret = 0;
	for (int i = 0;i<mBirdObjList.size();i++)
	{
		if (mBirdObjList[i]->GetTrackBirdState() == e)
		{
			ret = ret+1;
		}
	}
	return ret;
}
void TrackBirdMgr::SetCurrentTrackedObjStates(eBirdState e)
{
	mBirdObjList[mCurrentCalledIndex]->SetTrackBirdState(e);
}
void TrackBirdMgr::SetLastControlPoint(QVector3D src)
{
	mBirdObjList[mCurrentCalledIndex]->SetBeizerLastPoint(src);
}
void TrackBirdMgr::Render()
{
	for (int i=0;i<mBirdObjList.size();i++)
	{
		mBirdObjList[MAX_TRACKED_COUNTS - i - 1]->Render();
	}
}
void TrackBirdMgr::Update(float _elapsedTime,KinectWinMgr *mkgr)
{
	for (int i=0;i<mBirdObjList.size();i++)
	{
		mBirdObjList[i]->Update(_elapsedTime,NULL);
	}
	mBirdObjList[mCurrentCalledIndex]->Update(_elapsedTime,mkgr);
	mBirdObjList[mCurrentCalledIndex]->UpdateSkeletonInfo(mCurrentTrackedInfo);
}

void TrackBirdMgr::Update(float _elapsedTime)
{
	for (int i=0;i<mBirdObjList.size();i++)
	{
		mBirdObjList[i]->Update(_elapsedTime,NULL);
	}
}
void TrackBirdMgr::Reset()
{
	for (int i=0;i<mBirdObjList.size();i++)
	{
		mBirdObjList[i]->ResetToGameInit();
	}
}
QVector3D TrackBirdMgr::GetCurrentTrackedObjPos()
{
	return mBirdObjList[mCurrentCalledIndex]->GetPosition();
}
eBirdState TrackBirdMgr::GetCurrentTrackedObjState()
{
	return mBirdObjList[mCurrentCalledIndex]->GetTrackBirdState();
}
void TrackBirdMgr::Rotate()
{
	mBirdObjList[mCurrentCalledIndex]->Rotate();
}
void TrackBirdMgr::SetTrackedSkeID(unsigned long src)
{
	mBirdObjList[mCurrentCalledIndex]->SetSkeletonID(src);
}
unsigned long TrackBirdMgr::GetTrackedObjSkeID()
{
	return mBirdObjList[mCurrentCalledIndex]->GetSkeletonID();
}
void TrackBirdMgr::SetRestObjPos()
{
	for (int j=0,i=mCurrentCalledIndex+1;i<MAX_TRACKED_COUNTS;i++,j++)
	{
		mBirdObjList[i]->SetPosition(mBirdInitPosList[j]);
	}
}